<template>
  <div class="departmentManagement">
    <div class="title">
      <h3>科室管理</h3>
      <el-button type="primary" @click="handleShowAddDepartment">增加科室</el-button>
    </div>
    <el-table
      ref="elTable"
      v-loading="listLoading"
      :data="list"
      :fit="true"
      element-loading-text="正在查询中。。。"
      @selection-change="selectChange"
    >
      <el-table-column type="selection" fixed="left" width="50" />
      <el-table-column label="序号" type="index" />
      <el-table-column label="科室" prop="name" />
      <el-table-column label="在线服务数量" prop="serviceCount" />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="text"
            class="deleteBtn"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="footer">
      <div class="footer-left">
        <el-button plain size="small" @click="allSelection">全选</el-button>
        <el-button plain size="small" @click="delSelectOrder">删除</el-button>
      </div>
      <el-pagination
        background
        :page-sizes="[10, 20, 30, 50, 100]"
        :page-size="limit"
        :pager-count="5"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="pageSizeChange"
        @current-change="currentPageChange"
      />
    </div>
    <el-dialog
      :visible="addDepartment"
      class="ele-addDepartment"
      :before-close="handleShowAddDepartment"
      width="30%"
    >
      <template slot="title">
        <span class="el-dialog-title">增加科室</span>
      </template>
      <div class="del-content">
        <el-row>
          <el-form ref="departmentForm" class="ele-form" :model="departmentForm" :rules="rules" label-position="left" label-width="92px">
            <el-form-item label="科室名称：" prop="departmentName">
              <el-col
                :span="20"
              ><el-input
                v-model="departmentForm.departmentName"
                maxlength="10"
                placeholder="请输入科室名称"
              /></el-col>
            </el-form-item>
          </el-form>
        </el-row>
      </div>
      <template slot="footer" class="del-footer">
        <el-button type="primary" @click="submitDepartment">提交</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
/* import Pagination from '@/components/Pagination'*/
import { getToken } from '@/utils/auth'
import { departmentList, addDepartment, deleteDepartment } from '@/api/service'
/** 科室管理 */
export default {
  name: 'DepartmentManagement',
  /* components: { Pagination },*/
  data() {
    return {
      page: 1,
      limit: 10,
      total: 0,
      delId: [],
      departmentName: '',
      list: [],
      listLoading: true,
      form: {
        shopId: localStorage.getItem('shopId')
      },
      departmentForm: {
        departmentName: ''
      },
      addDepartment: false,
      rules: {
        departmentName: [{ required: true, message: '请输入科室名称', trigger: 'blur' }]
      }
    }
  },

  computed: {
    /* 请求头部信息*/
    headers() {
      return {
        'X-Litemall-Admin-Token': getToken()
      }
    }
  },
  watch: {
    addDepartment: {
      handler(newValue, oldValue) {
        if (newValue == false) {
          this.departmentForm.departmentName = null
        }
      }
    }
  },
  created() {
    this.loadTableData()
  },
  methods: {
    /* getList() {
      this.listLoading = true
      departmentList(this.form).then(response => {
        this.list = response.data.data.data
        console.log(this.list)
        this.total = response.data.data.total
        this.listLoading = false
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },*/
    pageSizeChange(pageSize) {
      const vm = this

      vm.limit = pageSize
      vm.reloadTableData()
    },
    currentPageChange(page) {
      const vm = this

      vm.page = page
      vm.loadTableData()
    },
    reloadTableData() {
      const vm = this

      vm.page = 1
      vm.total = 0
      vm.list = []
      vm.loadTableData()
    },
    loadTableData() {
      const vm = this

      const data = {
        page: vm.page,
        limit: vm.limit,
        shopId: parseInt(localStorage.getItem('shopId'))
      }

      vm.listLoading = true

      departmentList(data).then(res => {
        vm.listLoading = false
        vm.total = res.data.data.total
        vm.list = res.data.data.data ? res.data.data.data : []
      }).catch(res => {
        vm.listLoading = false
        if (res && res.data && res.data.errno !== 0) {
          this.$message.error(`${res.data.errmsg}（${res.data.errno}）`)
        }
      })
    },
    handleFilter() {
      this.page = 1
      this.getList()
    },
    // 添加科室
    handleShowAddDepartment() {
      this.addDepartment = !this.addDepartment
      this.departmentName = ''
    },
    submitDepartment() {
      this.delId = []
      this.$refs.departmentForm.validate(v => {
        if (v) {
          const data = {
            shopId: this.form.shopId,
            departmentName: this.departmentForm.departmentName
          }
          addDepartment(data).then(response => {
            console.log(response.data.data)
            this.$message.success('添加科室成功')
            this.$refs.departmentForm.resetFields()
            this.addDepartment = false
            this.loadTableData()
          })
        }
      })
    },
    // 表格单选触发
    selectChange(row) {
      this.delId = []
      row.forEach(element => {
        this.delId.push(element.id)
        console.log(this.delId)
      })
    },
    // 全选
    allSelection() {
      this.$refs.elTable.toggleAllSelection()
    },
    delSelectOrder() {
      if (this.delId && this.delId.length === 0) {
        this.$message.error('请选择删除的科室')
      } else {
        /* deleteDepartment(this.delId).then(response => {
          this.$message.success('删除成功')
          this.getList()
        }).catch(response => {
          this.$message.success('删除异常')
        })*/
        this.$confirm('是否删除所选科室?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteDepartment(this.delId).then(response => {
            this.$notify.success({
              title: '成功',
              message: '删除成功'
            })
            this.loadTableData()
          })
        }).catch(() => {
          this.$notify({
            type: 'info',
            message: '已取消删除'
          })
        })
      }
    },

    handleDelete(row) {
      const idList = [row.id]
      console.log(idList)
      this.$confirm('是否删除科室?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteDepartment(idList).then(response => {
          this.$notify.success({
            title: '成功',
            message: '删除成功'
          })
          this.loadTableData()
        })
      }).catch(() => {
        this.$notify({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.departmentManagement {
  padding: 50px 100px;
}
.title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 50px;
  ::v-deep .el-button {
    height: 36px;
  }
}
.deleteBtn {
  color: #ff0000;
}
::v-deep .el-table__body .el-table-column--selection .cell {
  padding-left: 10px;
}
.footer {
  display: flex;
  justify-content: space-between;
  margin: 50px 0px 100px 0px;
  .footer-left {
    ::v-deep.el-button {
      width: 100px;
      margin: 0px;
    }
  }
}
.ele-addDepartment {
  /*::v-deep.el-dialog > .el-dialog__header {
    text-align: center;
  }*/
  .el-dialog-title {
    width: 100%;
    font-size: 20px;
    font-weight: bold;
    text-align: center;
  }
  .del-shop-down {
    color: red;
    line-height: 20px;
    font-size: 16px;
  }
  .del-content {
    padding: 0px 20px;
    .ele-font-del {
      margin: 20px 0px;
    }
  }
  ::v-deep.el-dialog__footer {
    display: flex;
    justify-content: space-around;
    .el-button {
      width: 200px;
      height: 50px;
    }
  }
}
</style>
